###########################################################################################
#Replication code for Figures in Appendix A                                               #
#Political Trust and Government Performance in the Time of COVID-19                       #                                 #
###########################################################################################

#####################################################################
##FigureA.4
rm(list=ls(all=TRUE))
library(readstata13)
library(tidyverse)

setwd(dirname(rstudioapi::getSourceEditorContext()$path))

data <- read.dta13("data_constructed_trust.dta",convert.factors = F)

pdf("FigureA.4.pdf",width=6, height=4)
data %>%
  ggplot(aes(x=trust)) + geom_density(alpha=.2, colour="black",fill="#3690c0") +
  xlab("Political trust (country-year)")+ylab("Density")+
  scale_x_continuous(expand=c(0.01,0))+
  scale_y_continuous(expand=c(0.01,0))+
  theme_bw()
dev.off()

#####################################################################
##FigureA.5
rm(list=ls(all=TRUE))
library(sf)
library(readstata13)
library(RColorBrewer)

world <- st_read("worldmap")
world <- world[!world$country2=="Antarctica",]

data <- read.dta13("data_constructed_trust.dta",convert.factors = F)
data <- data%>% filter(year==2019) %>%
  select(stdtr,ccodealp)

worlddata <- left_join(world, data, by = c("adm0_a3_us"="ccodealp"))
worlddata$stdtr <- gtools::quantcut(worlddata$stdtr, q=10,
                                 labels=c("0-10%","10-20%","20-30%","30-40%","40-50%",
                                          "50-60%","60-70%","70-80%","80-90%","90-100%"))

colourPalette <- brewer.pal(10,'RdYlGn')
ppi <- 600
png("FigureA.5.png",width=10*ppi, height=6*ppi, res=ppi)
ggplot() +
  geom_sf(data = worlddata,aes(fill = stdtr),colour = "black",size=0.1,alpha=0.7)+
  labs(x=NULL, y=NULL)+
  coord_sf(crs = 4326)+
  scale_fill_manual(name="",values=colourPalette, na.value = "white")+
  guides(fill = guide_legend(nrow = 1))+
  theme(panel.border = element_blank(),
        legend.position="bottom",
        panel.grid = element_blank(),
        panel.background = element_rect(fill = "white"),
        axis.text.x = element_blank(), 
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(size = 12, hjust = 0.5),
        plot.margin = margin(t = 0,r = 0,b = 0,l = 0))
dev.off()

#####################################################################
##FigureA.7
rm(list=ls(all=TRUE))
library(openxlsx)

data<-read.xlsx("data_constructvalidity.xlsx")
data<-na.omit(data)

pdf("FigureA.7.pdf", width=6, height=4)
ggplot(data,aes(x=gallup,y=constructed_trust))+
  geom_point()+geom_smooth(method=lm)+xlab("Observed political trust from Gallup")+
  ylab("Constructed political trust")+
  annotate(geom="text", x=0.125, y=2,hjust = 0,size=5,
           label  = "paste(rho==0.723, \" *** \")",
           parse = TRUE)+theme_bw()
dev.off()
